﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Stitch.Database;

namespace Data.GameData.Entities
{
    public class ESystemBody : EBaseObject
    {
        public static ESystemBody LoadFromDB(Guid id)
        {
            string sqlStatement = "Select * from SystemBody where id = '" + id.ToString() + "'";
            RunSQL(sqlStatement);

            DBRow dbRow = databaseResult.FetchArray();

            if (!databaseResult.MsSqlReader.HasRows)
                throw new Exception("No selected rows");
            if (databaseResult.MsSqlReader.FieldCount > 1)
                throw new Exception("Too many rows");

            ESystemBody newObject = new ESystemBody();
            newObject.values = dbRow;

            return newObject;
        }

        internal static List<ESystemBody> GetAll()
        {
            string sqlStatement = "Select "
                + "SystemBody.id, SystemBody.imageName, SystemBody.heading, SystemBody.mapId, SystemBody.row, "
                + "SystemBody.col, SystemBody.ownerId, SystemBody.starSystemId, SystemBody.type, SystemBody.enviroment, "
                + "SystemBody.starType, SystemBody.mass, SystemBody.rei, SystemBody.habRating, SystemBody.radius, "
                + "SystemBody.initialBearing, SystemBody.orbitsId, SystemBody.populationLinkId "
                + "From SystemBody, StarSystem "
                + "Where '"
                + Game.ActiveGame.Id.ToString()
                + "' = StarSystem.gameId And StarSystem.id = SystemBody.starSystemId";

            RunSQL(sqlStatement);

            List<ESystemBody> eEntities = new List<ESystemBody>();

            DBRow dbRow = databaseResult.FetchArray();

            while (dbRow != null)
            {
                ESystemBody newObject = new ESystemBody();
                newObject.values = dbRow;
                eEntities.Add(newObject);

                dbRow = databaseResult.FetchArray();
            }

            return eEntities;
        }

        internal void Delete()
        {
            string sqlStatement = "DELETE FROM SystemBody WHERE id = '" + values["id"].ToString() + "'";
            RunNonSelectSQL(sqlStatement);
        }

        internal void SaveToDB(bool isNew)
        {
            string sqlStatement;

            if (isNew)
            {
                sqlStatement = "INSERT INTO SystemBody ("
                    + "id, imageName, heading, mapId, row, col, ownerId, starSystemId,"
                    + "type, enviroment, starType, mass, rei, habRating, radius, initialBearing, orbitsId) VALUES ("
                    + "'" + values["id"] + "', "
                    + "'" + values["imageName"] + "', "
                    + values["heading"] + ", "
                    + values["mapId"] + ", "
                    + values["row"] + ", "
                    + values["col"] + ", "
                    + "'" + values["ownerId"] + "', "
                    + "'" + values["starSystemId"] + "', "
                    + "'" + values["type"] + "', "
                    + "'" + values["enviroment"] + "', "
                    + "'" + values["starType"] + "', "
                    + values["mass"] + ", "
                    + values["rei"] + ", "
                    + values["habRating"] + ", "
                    + values["radius"] + ", "
                    + values["initialBearing"] + ", "
                    + "'" + values["orbitsId"] + "'"
                    + ")";
            }
            else
            {
                sqlStatement = "UPDATE SystemBody SET "
                    + "'" + values["imageName"] + "', "
                    + values["heading"] + ", "
                    + values["mapId"] + ", "
                    + values["row"] + ", "
                    + values["col"] + ", "
                    + "'" + values["ownerId"] + "', "
                    + "'" + values["starSystemId"] + "', "
                    + "'" + values["type"] + "', "
                    + "'" + values["enviroment"] + "', "
                    + "'" + values["starType"] + "', "
                    + values["mass"] + ", "
                    + values["rei"] + ", "
                    + values["habRating"] + ", "
                    + values["radius"] + ", "
                    + values["initialBearing"] + ", "
                    + "'" + values["orbitsId"] + "'"
                    + " WHERE id = '" + values["id"] + "'";
            }

            RunNonSelectSQL(sqlStatement);
        }
    }
}
